package com.ruoyi.biz.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.biz.domain.BizGame;
import com.ruoyi.biz.pojo.vo.BizPanVO;
import com.ruoyi.biz.service.IBizGameService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.domain.BizPan;
import com.ruoyi.biz.service.IBizPanService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 游戏网盘Controller
 *
 * @author jason
 * @date 2025-10-31
 */
@Controller
@RequestMapping("/biz/pan")
public class BizPanController extends BaseController {
    private String prefix = "biz/pan";

    @Autowired
    private IBizPanService bizPanService;

    @Autowired
    IBizGameService bizGameService;

    @RequiresPermissions("biz:pan:view")
    @GetMapping()
    public String pan() {
        return prefix + "/pan";
    }

    /**
     * 查询游戏网盘列表
     */
    @RequiresPermissions("biz:pan:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizPan bizPan) {
        startPage();
        List<BizPan> list = bizPanService.selectBizPanList(bizPan);
        List<BizPanVO> vos = BeanUtil.copyToList(list, BizPanVO.class);

        List<Long> ids = list.stream().map(BizPan::getGameId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(ids)) {
            List<BizGame> bizGames = bizGameService.listByIds(ids);
            Map<Long, String> map = bizGames.stream().collect(Collectors.toMap(BizGame::getId, BizGame::getName));

            vos.forEach(v -> {
                if (map.containsKey(v.getGameId())) {
                    v.setGameName(map.get(v.getGameId()));
                }
            });

        }

        return getDataTable(vos);
    }

    /**
     * 导出游戏网盘列表
     */
    @RequiresPermissions("biz:pan:export")
    @Log(title = "游戏网盘", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizPan bizPan) {
        List<BizPan> list = bizPanService.selectBizPanList(bizPan);
        ExcelUtil<BizPan> util = new ExcelUtil<BizPan>(BizPan.class);
        return util.exportExcel(list, "游戏网盘数据");
    }

    /**
     * 新增游戏网盘
     */
    @RequiresPermissions("biz:pan:add")
    @GetMapping("/add")
    public String add(ModelMap mmp) {
        // 获取游戏下拉
        List<BizGame> games = bizGameService.selectBizGameList(new BizGame());
        mmp.put("games", games);
        return prefix + "/add";
    }

    /**
     * 新增保存游戏网盘
     */
    @RequiresPermissions("biz:pan:add")
    @Log(title = "游戏网盘", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizPan bizPan) {
        return toAjax(bizPanService.insertBizPan(bizPan));
    }

    /**
     * 修改游戏网盘
     */
    @RequiresPermissions("biz:pan:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BizPan bizPan = bizPanService.selectBizPanById(id);
        mmap.put("bizPan", bizPan);
        // 获取游戏下拉
        List<BizGame> games = bizGameService.selectBizGameList(new BizGame());
        mmap.put("games", games);

        return prefix + "/edit";
    }

    /**
     * 修改保存游戏网盘
     */
    @RequiresPermissions("biz:pan:edit")
    @Log(title = "游戏网盘", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizPan bizPan) {
        return toAjax(bizPanService.updateBizPan(bizPan));
    }

    /**
     * 删除游戏网盘
     */
    @RequiresPermissions("biz:pan:remove")
    @Log(title = "游戏网盘", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bizPanService.deleteBizPanByIds(ids));
    }
}
